import 'dart:convert';

import 'package:drive/drive.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mvc_example/common/empty.dart';
import 'package:flutter_mvc_example/common/res.dart';

import 'bean/good_bean.dart';
import 'bean/shop_bean.dart';
import 'pop/controller.dart';
import 'view.dart';
import 'package:flutter_mvc/flutter_mvc.dart';

///Description:商品详情
///Author:teihideharu
///date created 2021/04/27
class GoodController extends BaseController {
  GoodDataBean data;
  ShopDataBean shopData;

  var titleBarOpacity = 0.0;
  final imageHeight = 375.dp;
  final goodId;
  final String defaultImage;
  final String defaultTitle;
  final String defaultPrice;
  final String defaultShopName;
  final String tagEnd;
  bool isDetails = false;

  GoodController(this.goodId,
      {this.defaultImage = "",
        this.tagEnd = "",
        this.defaultTitle = "",
        this.defaultPrice = "",
        this.defaultShopName = ""})
      : super(GoodPage());
  final scrollController = ScrollController();

  @override
  void initState() {
    super.initState();
    httpGood();
    scrollController.addListener(onScrollListener);
  }

  final detailsHeight = 800.dp;

  void onScrollListener() {
    final offset = scrollController.offset;
    final isDetails = offset > detailsHeight;
    if (isDetails != this.isDetails) {
      this.isDetails = isDetails;
      setState();
    }
    if (titleBarOpacity == 1 && offset > imageHeight) {
      return;
    }
    if (titleBarOpacity == 0 && offset < 0) {
      return;
    }
    titleBarOpacity = offset / imageHeight;
    if (titleBarOpacity < 0) {
      titleBarOpacity = 0;
    } else if (titleBarOpacity > 1) {
      titleBarOpacity = 1;
    }
    setState();
  }


  Future pushHero(BuildContext context, {routeBuilder}) {
    return Navigator.of(context).push(
      PageRouteBuilder<Null>(
          pageBuilder: (BuildContext context, Animation<double> anim1,
              Animation<double> anim2) {
            return Semantics(
                button: true,
                child: FadeTransition(
                  opacity: anim1,
                  child: widget,
                ));
            return AnimatedBuilder(
              animation: anim1,
              child: widget,
              builder: (BuildContext context, Widget child) {
                return Opacity(
                  opacity: anim1.value,
                  child: child,
                );
              },
            );
          },
          transitionDuration: Duration(milliseconds: 500),
          reverseTransitionDuration: Duration(milliseconds: 500)),
    );
  }

  void onPicClick(int index) {
  }

  void onHomeClick() {
    ToastUtils.show("返回首页被点击事件");
    // MvcNavigatorManager.manager.popAllAndPush(context);
  }

  void httpGood() async {
    // showLoading();
    final bean = GoodBean(JsonDecoder().convert(empty_goods_json));
    // dismissLoading();
    if (bean.code == 0) {
      data = bean.data;
      data.description = empty_goods_description;
      trimGoodsData();
      httpShop(data.shopId);
      setState();
    } else {
      ToastUtils.show(bean.msg);
    }
  }

  //接口太懒，被迫整理数据
  void trimGoodsData() {
    GoodDataSkuThisBean getData(String name) {
      for (var value in data.thisSku) {
        if(value.name==name){
          return value;
        }
      }
      return null;
    }

    void addSku(GoodDataSkuBean element) {
      final value=getData(element.propVal.first);
      if(value!=null){
        value.child.add(element);
      }else{
        final newThisBean=GoodDataSkuThisBean();
        newThisBean.name=element.propVal.first;
        newThisBean.img=element.propImg;
        newThisBean.num=0;
        newThisBean.child.add(element);
        data.thisSku.add(newThisBean);
      }
    }

    data.sku.forEach((element) {
      addSku(element);
    });
  }

  void httpShop(String shopId) async {
    showLoading();
    final bean = ShopBean(JsonDecoder().convert(empty_shop_json));
    dismissLoading();
    if (bean.code == 0) {
      shopData = bean.data;
    }
    setState();
  }

  void onValueClick() {
    GoodPopController(GoodPopController.value, goodId, data).push(context);
  }

  void onSkuClick() {
    GoodPopController(GoodPopController.sku, goodId, data).push(context);
  }

  void onContactClick() {
    GoodPopContactController(shopData).push(context);
  }

  onChooseModelClick() async {
    ToastUtils.show("加入选款被点击事件");
  }

  onShopClick() {
    ToastUtils.show("加入选款被点击事件");
  }
}
